package com.smarttourism.service;

import com.smarttourism.Utils.ShxUtils;
import com.smarttourism.domain.Checkin;
import com.smarttourism.domain.Customer;
import com.smarttourism.domain.Roomorder;
import com.smarttourism.mapper.CheckinMapper;
import com.smarttourism.mapper.CustomerMapper;
import com.smarttourism.mapper.RoomorderMapper;
import com.smarttourism.mapper.SHXRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CheckInService {
    @Autowired
    private CheckinMapper checkinMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RoomorderMapper roomorderMapper;
    @Autowired
    private SHXRoomMapper shxRoomMapper;

    @Transactional
    public int addCherkIn(Checkin checkin) throws ParseException {
        // 保存到入住登记表中的旅客信息
        List<Integer> customerId = new ArrayList<>();
        // 将旅客信息添加到旅客表中
        List<Customer> customers = checkin.getCustomers();
        for (Customer customer : customers) {
            // 判断该旅客的信息旅客表中是否存在
            Integer i = customerMapper.selectIdByCustomerIdCard(customer.getIdcard());

            if (i!=null){
                // 表示旅客信息存在存在则修改旅客状态为常客
                customerMapper.updateCrangeTo2ByIdCard(customer.getIdcard());
                // 将该旅客的id保存到入住登记表中的旅客信息中
                customerId.add(i);
            }else {
                // 如果旅客信息不存在，则添加旅客信息
                // 将旅客身份证信息转化为生日
                Date date = ShxUtils.extractBirthdayFromIdCard(customer.getIdcard());
                customer.setBirthday(date);
                customer.setCrange(1);
                customer.setDel(0);
                customer.setNation("汉族");
                // 将旅客信息添加到 旅客表中
                customerMapper.insertSelective(customer);
                customerId.add(customerMapper.selectIdByCustomerIdCard(customer.getIdcard()));
            }
        }
        Integer[] array = customerId.toArray(new Integer[0]);
        String customerIds = ShxUtils.convertIntArrayToString(array);
        checkin.setCustomerId(customerIds);
        // 修改订单状态为已完成4
        roomorderMapper.updateOrderStatusById(4,checkin.getOrderId());
        // 修改房间状态为游客2
        Roomorder roomorder = roomorderMapper.selectByPrimaryKey(Long.valueOf(checkin.getOrderId()));
        shxRoomMapper.updateStatusByNum(2,roomorder.getRNum());
        // 增加入住登记表
        int i = checkinMapper.insert(checkin);
        return i;
    }

    public List<Checkin> findAllCheckin(){
        List<Checkin> allCheckIn = checkinMapper.findAllCheckIn();
        for (Checkin checkin : allCheckIn) {
            if (checkin!=null){
                // 添加关联的订单对象
                checkin.setRoomorder(roomorderMapper.selectByPrimaryKey(Long.valueOf(checkin.getOrderId())));
                Roomorder roomorder = checkin.getRoomorder();
                roomorder.setRoomInfo(shxRoomMapper.selectRoomByRnum(roomorder.getRNum()));
                checkin.setRoomorder(roomorder);
                // 添加关联的旅客对象
                int[] ints = ShxUtils.convertStringToIntArray(checkin.getCustomerId());
                List<Customer> list = new ArrayList<>();
                for (int i : ints) {
                    Customer customer = customerMapper.selectByPrimaryKey(Long.valueOf(i));
                    customer.setCustomerRange(customerMapper.findCustomerRangeByRangeId(customer.getCrange()));
                    customer.setCustomerIdType(customerMapper.findCustomerIdTypeByTypeId(customer.getIdType()));
                    list.add(customer);
                }
                checkin.setCustomers(list);
            }
        }
        return allCheckIn;
    }

    // 退房业务
    @Transactional
    public int checkOut(Integer checkInId,Integer roomNum){
        // 修改入住记录为已退房
        checkinMapper.updateStatusById("已退房",checkInId);
        // 修改房间状态为脏房4
        shxRoomMapper.updateStatusByNum(4,roomNum);
        return 1;
    }

    public int deleteCheckIn(Integer id){
        return checkinMapper.updateDelById(id);
    }

}
